<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * -----------------------------------------------------
 *
 * controllers member
 *
 * @package     controllers
 * @subpackage  
 * @category    Info
 * @author      Pupuchen
 * @since       2015-7-9 version 1.0       
 * -----------------------------------------------------    
 */
class Info extends CI_Controller {

	private $view_url = 'home/member/info/'; // 视图url

	public function __construct()
	{
	  	parent::__construct();
	  	$this->load->model(array('member_model','industry_model','region_model'));
	  	$this->load->library('my_md5'); 
	}
	 
	/**
	 * [index 会员中心首页]
	 * @return [type] [description]
	 */
	public function index()
	{	
		$info     = $this->member_model->get_a_member_by_mid($_SESSION['member_id']);
		if($info['industry'] > 0){
			$industry = $this->industry_model->to_options(0,0,$info['industry']);// 加载所有行业
		}else{
			$industry = $this->industry_model->to_options(0,0);// 加载所有行业
		}

		if($info['province'] > 0){
			$province = $this->region_model->get_datelist_option(1,$info['province']);// 加载省数据
			$city     = $this->region_model->get_datelist_option($info['province'],$info['city']);// 加载市数据
			$area     = $this->region_model->get_datelist_option($info['city'],$info['area']);// 加载区数据
		}else{
			$province = $this->region_model->get_datelist_option(1);// 加载省数据
			$city	  = '';
			$area     = '';
		}
	
		$this->load->view($this->view_url.'index',compact("industry","province","info","province","city","area"));
	}

	/**
	 * [changemobile 修改手机号码]
	 * @return [type] [description]
	 */
	public function changemobile(){
		$this->load->view($this->view_url.'mobile');
	}

	/**
	 * [changepassword 修改密码]
	 * @return [type] [description]
	 */
	public function changepassword(){
		$this->load->view($this->view_url.'password');
	}

	/**
	 * [setinfo 更新会员信息]
	 * @return [type] [description]
	 */
	public function setinfo(){
		$result = array(
            'err'=>'0',
            'msg'=>''
        );
		$data = $this->input->post();
		if($data['type'] == 'mobile'){
			// 检查登录密码是否正确
			$arr_member = $this->member_model->get_a_member_by_mid($_SESSION['member_id']); // 会员信息 
			// 比对密码
			if($this->my_md5->md5_2($_POST['password']) != $this->my_md5->get_md5($arr_member['password'])){
				$result['err'] = 1;
	            $result['msg'] = '密码不正确,请重新输入!';
	            exit(json_encode($result));
			}
	    	$mode='/^1[0-9]{10,10}$/'; // 手机号码匹配
	    	if(!preg_match($mode,$_POST['mobile'],$arr)){
	        	$result['err'] = 2;
	            $result['msg'] = '手机号码格式不正确,请重新输入!';
	            exit(json_encode($result));
	    	}
	    	unset($data['password']);
	    	$data['mobile'] = $_POST['mobile'];
		}

		if($data['type'] == 'password'){
			// 检查登录密码是否正确
			$arr_member = $this->member_model->get_a_member_by_mid($_SESSION['member_id']); // 会员信息 
			// 比对密码
			if($this->my_md5->md5_2($data['oldpassword']) != $this->my_md5->get_md5($arr_member['password'])){
				$result['err'] = 3;
	            $result['msg'] = '旧密码输入不正确,请重新输入!';
	            exit(json_encode($result));
			}
	    	unset($data['oldpassword']);
	    	$data['password'] = $this->my_md5->to_md5($data['password']);
		}

		unset($data['type']);
		if($this->member_model->update($data,$_SESSION['member_id'])){
			$result['err'] = 0;
            exit(json_encode($result));
		}else{
			$result['err'] = 2;
            $result['msg'] = '保存错误，请稍后保存!';
            exit(json_encode($result));
		}
	}


}